import Vue from 'vue'
import VueRouter from 'vue-router'

import {SESSION_KEY_TOKEN} from '../utils/CommonConsts'
// import Login from '../components/Login'
// import Home from '../components/Home'
// import Welcome from '../components/Welcome'
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Login')
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Home')
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Welcome')

// import Users from '../components/user/Users'
// import Rights from '../components/power/Rights'
// import Roles from '../components/power/Roles'

const Users = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/user/Users')
const Rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Rights')
const Roles = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Roles')

// import Cate from '../components/goods/Cate'
// import Params from "../components/goods/Params";
const Cate = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Cate')
const Params = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Params')

// import GoodList from "../components/goods/List";
// import Add from "../components/goods/Add";
const GoodList = () => import(/* webpackChunkName: "GoodList_Add" */ '../components/goods/List')
const Add = () => import(/* webpackChunkName: "GoodList_Add" */ '../components/goods/Add')

// import Order from "../components/order/Order";
// import Report from "../components/report/Report";
const Order = () => import(/* webpackChunkName: "Order_Report" */ '../components/order/Order')
const Report = () => import(/* webpackChunkName: "Order_Report" */ '../components/report/Report')



Vue.use(VueRouter)

const routes = [
  /* 路由重定向 */
  {path: '/', redirect: '/login'},
  {path: '/login', component: Login},
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {path: '/welcome', component: Welcome},
      {path: '/users', component: Users},
      {path: '/rights', component: Rights},
      {path: '/roles', component: Roles},
      {path: '/categories', component: Cate},
      {path: '/params', component: Params},
      {path: '/goods', component: GoodList},
      {path: '/goods/add', component: Add},
      {path: '/orders', component: Order},
      {path: '/reports', component: Report}
    ]
  }
]

const router = new VueRouter({
  routes
})

/**
 *  挂载路由导航守卫,控制页面访问权限
 *  @param to 将要访问的路由（Route）对象
 *  @param from 代表从哪个 路由（Route） 跳转而来
 *  @param next 是一个函数，表示放行  调用方式： next() 放行  next('/login') 强制跳转
 */
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  }
  // 获取token
  const tokenStr = window.sessionStorage.getItem(SESSION_KEY_TOKEN)
  if (!tokenStr) { // 说明没登录
    return next('/login')
  }
  next()
})

export default router
